//
//  TYMBHeadSliderView.m
//  TY_MotorRemoteControl
//
//  Created by 徐伟新 on 2024/3/22.
//  Copyright © 2024 zhongdaodianzi. All rights reserved.
//

#import "TYMBHeadSliderView.h"

@implementation TYMBHeadSliderView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (void)layoutSubviews {
    
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.height*2.02142857/3.4);
    }];
    
    
    
    [self.topImgV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(-self.height/4.0);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(self.height/4.0);
        make.width.mas_equalTo(self.topImgV.mas_height).multipliedBy(11.0);
    }];
    
    [self.bottomImgV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.height/4.0);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(self.height*0.15);
        make.width.mas_equalTo(self.bottomImgV.mas_height).multipliedBy(20.17647);
    }];
}

- (void)setUpUI {
//    make.left.mas_equalTo(-self.height*2.02142857/2.0);

    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(4.0);
        make.left.mas_equalTo(-self.height*2.02142857/3.4);
    }];
    
    [self.showView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(self.bgView.width*self.precent);
    }];
    
    [self.topImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(-self.height/4.0);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(self.height/4.0);
        make.width.mas_equalTo(self.topImgV.mas_height).multipliedBy(11.0);

    }];
    
    [self.bottomImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.height/4.0);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(self.height*0.15);
        make.width.mas_equalTo(self.bottomImgV.mas_height).multipliedBy(20.17647);

    }];
    
    
    [self.thumImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(self.thumImgV.mas_height).multipliedBy(2.02142857);
        make.centerX.mas_equalTo(self.showView.mas_right);
    }];
    
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    CGPoint point = [[touches anyObject]locationInView:self.bgView];
    if (point.x < 0) {
        self.precent = 0;
    }else if (point.x > self.bgView.width) {
        self.precent = 1.0;
    }else {
        self.precent = point.x /self.bgView.width;
    }
    
}


- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject]locationInView:self.bgView];
    if (point.x < 0) {
        self.precent = 0;
    }else if (point.x > self.bgView.width) {
        self.precent = 1.0;
    }else {
        self.precent = point.x /self.bgView.width;
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject]locationInView:self.bgView];
    if (point.x < 0) {
        self.precent = 0;
    }else if (point.x > self.bgView.width) {
        self.precent = 1.0;
    }else {
        self.precent = point.x /self.bgView.width;
    }
    
    if (self.precent >= 0.5) {
        self.precent = 1.0;
    }else {
        self.precent = 0.0;
    }
    
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject]locationInView:self.bgView];
    if (point.x < 0) {
        self.precent = 0;
    }else if (point.x > self.bgView.width) {
        self.precent = 1.0;
    }else {
        self.precent = point.x /self.bgView.width;
    }
    if (self.precent >= 0.5) {
        self.precent = 1.0;
    }else {
        self.precent = 0.0;
    }
    
}


- (void)setPrecent:(CGFloat)precent {
    _precent = precent;
    [self.showView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.bgView.width*self.precent);

    }];
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = UIColorFromHex(0x00ff00);
        [self addSubview:_bgView];
    }
    return _bgView;
}

- (UIView *)showView {
    if (!_showView) {
        _showView = [[UIView alloc]init];
        _showView.backgroundColor = UIColorFromHex(0xff0000);
        [self.bgView addSubview:self.showView];
    }
    return _showView;
}

- (UIImageView *)topImgV {
    if (!_topImgV) {
        _topImgV = [[UIImageView alloc]init];
        _topImgV.image = [UIImage imageNamed:@"word"];
        [self addSubview:_topImgV];
    }
    return _topImgV;
}

- (UIImageView *)bottomImgV {
    if (!_bottomImgV) {
        _bottomImgV = [[UIImageView alloc]init];
        _bottomImgV.image = [UIImage imageNamed:@"word_2"];
        [self addSubview:_bottomImgV];
    }
    return _bottomImgV;
    
}

- (UIImageView *)thumImgV {
    if (!_thumImgV) {
        _thumImgV = [[UIImageView alloc]init];
        _thumImgV.image = [UIImage imageNamed:@"logo"];
        [self insertSubview:_thumImgV aboveSubview:self.bgView];
    }
    return _thumImgV;
    
}


@end
